<?php

class Anwser extends Controller {

    public function __construct() {
        parent::Controller();
        if (!$this->session->userdata('isAuthenticated')) {
            redirect(base_url.'admin.php?c=auth&m=index');
        }
        $this->load->model('anwsermodel', 'anwser');
    }

    public function index() {
        $this->load->library('pagination');

        $limit = 2;
        $offset = intval($this->input->get('id'));
        $total = $this->anwser->a_count();

        $config['base_url'] = base_url() . 'admin.php?c=anwser&m=index';
        $config['total_rows'] = $total;
        $config['per_page'] = $limit;
        $this->pagination->initialize($config);

        $data['title'] = '在线咨询';
        $data['anwsers'] = $this->anwser->a_list($limit, $offset);
        $data['page_links'] = $this->pagination->create_links();
        $this->load->view('anwser/index', $data);
    }

    public function edit() {
        $this->load->model('questionmodel', 'question');
        $this->load->helper('form');
        $id = intval($this->input->get('id'));
        if ($a = $this->anwser->find($id)) {
            $data['entry'] = $a;
            $data['question'] = $this->question->find($a->question_id);
            $this->load->view('anwser/edit', $data);
        } else {
            show_404();
        }
    }

    public function update() {
        $id = intval($this->input->post('id'));
        if ($a = $this->anwser->find($id)) {
            $this->load->library('form_validation');
            $this->load->model('questionmodel', 'question');
            if ($this->form_validation->run() == false) {
                $data['entry'] = $a;
                $data['question'] = $this->question->find($a->question_id);
                $this->load->view('anwser/edit', $data);
            } else {
                $anwser['question_id'] = $a->question_id;
                $anwser['author'] = $this->input->post('author');
                $anwser['content'] = $this->input->post('a_content');
                if ($this->db->update('anwser', $anwser, array('id' => $id))) {
                    $this->session->set_flashdata('notice', '回复成功');
                    redirect(base_url().'admin.php?c=question&m=reply&id=' . $a->question_id);
                } else {
                    $this->session->set_flashdata('error', 'db error!');
                    redirect(base_url().'admin.php?c=question&m=reply&id=' . $a->question_id);
                }
            }
        } else {
            show_404();
        }
    }

    public function delete() {
        $id = intval($this->input->get('id'));
        if ($anwser = $this->anwser->find($id)) {
            if ($this->db->delete('anwser', array('id' => $id))) {
                $this->session->set_flashdata('notice', '删除成功！');
            } else {
                $this->session->set_flashdata('error', 'db error!');
            }
            redirect(base_url().'admin.php?c=question&m=reply&id=' . $a->question_id);
        } else {
            show_404();
        }
    }

}
